package ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import base.utils.HttpClient;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class FtpUtil {
    private static String hostName="192.168.109.111";
    private static int port=21;
    private static String userName="lxb";
    private static String passWord="123456";
    private static FTPClient ftpClient=new FTPClient();
    static {
        //连接FTP服务器
        try {
              //ceshis
            ftpClient.setControlEncoding("UTF-8");
            ftpClient.connect(hostName, port);
            //登录FTP服务器
            ftpClient.login(userName, passWord);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 上传文件（可供Action/Controller层使用）
     * @param pathname  FTP服务器保存目录
     * @param fileName  上传到FTP服务器后的文件名称
     * @param inputStream 输入文件流
     * @return
     */
    public static boolean uploadFile( String pathname, String fileName, InputStream inputStream){
        boolean flag = false;

        try {
            int replyCode = ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(replyCode)){
                return flag;
            }

            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.makeDirectory(pathname);
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.storeFile(fileName, inputStream);
            inputStream.close();
            ftpClient.logout();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if(ftpClient.isConnected()){
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }


    /**
     * 上传文件（可对文件进行重命名）
     * @param pathname  FTP服务器保存目录
     * @param filename  上传到FTP服务器后的文件名称
     * @param originfilename 待上传文件的名称（绝对地址）
     * @return
     */
    public static boolean uploadFileFromProduction( String pathname, String filename, String originfilename){
        boolean flag = false;
        try {
            InputStream inputStream = new FileInputStream(new File(originfilename));
            flag = uploadFile( pathname, filename, inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 上传文件（不可以进行文件的重命名操作）

     * @param pathname  FTP服务器保存目录
     * @param originfilename 待上传文件的名称（绝对地址）
     * @return
     */
    public static boolean uploadFileFromProduction( String pathname, String originfilename){
        boolean flag = false;
        try {
            String fileName = new File(originfilename).getName();
            InputStream inputStream = new FileInputStream(new File(originfilename));
            flag = uploadFile(pathname, fileName, inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    /**
     * 删除文件
     * @param pathname  FTP服务器保存目录
     * @param filename  要删除的文件名称
     * @return
     */
    public static boolean deleteFile( String pathname, String filename){
        boolean flag = false;
        try {
            //验证FTP服务器是否登录成功
            int replyCode = ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(replyCode)){
                return flag;
            }
            //切换FTP目录
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.dele(filename);
            ftpClient.logout();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if(ftpClient.isConnected()){
                try {
                    ftpClient.logout();
                } catch (IOException e) {

                }
            }
        }
        return flag;
    }

    /**
     * 下载文件
     * @param pathname  FTP服务器文件目录
     * @param filename  文件名称
     * @param localpath 下载后的文件路径
     * @return
     */
    public static boolean downloadFile(String pathname, String filename, String localpath){
        boolean flag = false;
        try {
            //验证FTP服务器是否登录成功
            int replyCode = ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(replyCode)){
                return flag;
            }
            //切换FTP目录
            ftpClient.changeWorkingDirectory(pathname);

                    File localFile = new File(localpath + "/" + filename);
                    OutputStream os = new FileOutputStream(localFile);
                    ftpClient.retrieveFile(filename, os);
                    os.close();

            ftpClient.logout();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if(ftpClient.isConnected()){
                try {
                    ftpClient.logout();
                } catch (IOException e) {

                }
            }
        }
        return flag;
    }

    /**
     * @param pathname FTP文件服务器目录
     * @param remote 目标文件
     * @return
     */
    public InputStream getInputStream(String pathname,String remote){
        InputStream inputStream = null;
        //切换FTP目录
        try {
            ftpClient.changeWorkingDirectory(pathname);
            inputStream= ftpClient.retrieveFileStream(remote);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
                ftpClient.logout();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream;
    }
    public static void main(String[] args) {
        FtpUtil.downloadFile("","test.jpg","C:\\Users\\lxb\\Desktop");
        //FtpUtil.uploadFileFromProduction("files/media","003.jpg","E:\\\\wadp\\\\image\\\\basket\\\\001.jpg");
    }

}